package demo.java.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;

import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;

/**
 * @author wangly on @date 2023/7/1
 */
public class JSONToCSVUtil {
    String csvFile = "D:\\work\\智能审核\\20230830\\性能日志\\facade-8000-all.csv";
    String logFile = "D:\\work\\智能审核\\20230830\\性能日志\\facade-8000-all.txt";

    @Test
    public void performanceLogsToCsv() throws IOException {
//        String head = "\"pid\",\"00-总执行时长\",\"01-数据处理\",\"01-数据处理-01取数\",\"01-数据处理-02合并入参\",\"01-数据处理-03数据转换\",\"01-数据处理-04费用冲正\",\"02-规则执行\",\"02-规则执行-insurance-1-1-1\",\"02-规则执行-insurance-1-1-2\",\"02-规则执行-insurance-1-1-3\",\"02-规则执行-insurance-1-2-1\",\"02-规则执行-insurance-1-3-1\",\"02-规则执行-insurance-2-1-1\",\"02-规则执行-insurance-2-1-2\",\"02-规则执行-insurance-2-1-3\",\"02-规则执行-insurance-2-1-4\",\"02-规则执行-insurance-2-1-5\",\"02-规则执行-insurance-2-1-7\",\"02-规则执行-insurance-3-1-1\",\"02-规则执行-insurance-3-1-10\",\"02-规则执行-insurance-3-1-11\",\"02-规则执行-insurance-3-1-2\",\"02-规则执行-insurance-3-1-3\",\"02-规则执行-insurance-3-1-4\",\"02-规则执行-insurance-3-1-5\",\"02-规则执行-insurance-3-1-6\",\"02-规则执行-insurance-3-1-8\",\"02-规则执行-insurance-3-1-9\",\"02-规则执行-insurance-3-2-1\",\"02-规则执行-insurance-3-2-2\",\"02-规则执行-insurance-3-2-3\",\"02-规则执行-insurance-3-2-4\",\"02-规则执行-insurance-3-3-1\",\"02-规则执行-insurance-3-3-2\",\"02-规则执行-insurance-3-3-3\",\"02-规则执行-insurance-x-1\",\"03-规则结果适配\",\"03-规则结果适配-01-违规过滤器\",\"03-规则结果适配-02-第三方过滤器\",\"03-规则结果适配-03-数据落库\"";
        String head = "\"pid\",\"00-总执行时长\",\"01-数据处理\",\"02-规则执行\",\"03-规则结果适配\",\"01-数据处理-01取数\",\"01-数据处理-02合并入参\",\"01-数据处理-03数据转换\",\"01-数据处理-04费用冲正\",\"02-规则执行-insurance-1-1-1\",\"02-规则执行-insurance-1-1-2\",\"02-规则执行-insurance-1-1-3\",\"02-规则执行-insurance-1-2-1\",\"02-规则执行-insurance-1-3-1\",\"02-规则执行-insurance-2-1-1\",\"02-规则执行-insurance-2-1-2\",\"02-规则执行-insurance-2-1-3\",\"02-规则执行-insurance-2-1-4\",\"02-规则执行-insurance-2-1-5\",\"02-规则执行-insurance-2-1-7\",\"02-规则执行-insurance-3-1-1\",\"02-规则执行-insurance-3-1-10\",\"02-规则执行-insurance-3-1-11\",\"02-规则执行-insurance-3-1-2\",\"02-规则执行-insurance-3-1-3\",\"02-规则执行-insurance-3-1-4\",\"02-规则执行-insurance-3-1-5\",\"02-规则执行-insurance-3-1-6\",\"02-规则执行-insurance-3-1-8\",\"02-规则执行-insurance-3-1-9\",\"02-规则执行-insurance-3-2-1\",\"02-规则执行-insurance-3-2-2\",\"02-规则执行-insurance-3-2-3\",\"02-规则执行-insurance-3-2-4\",\"02-规则执行-insurance-3-3-1\",\"02-规则执行-insurance-3-3-2\",\"02-规则执行-insurance-3-3-3\",\"02-规则执行-insurance-x-1\",\"03-规则结果适配-01-违规过滤器\",\"03-规则结果适配-02-第三方过滤器\",\"03-规则结果适配-03-数据落库\"";
//        String headMapStr = "{\"01-数据处理-02合并入参\":4,\"01-数据处理-03数据转换\":5,\"pid\":0,\"03-规则结果适配-01-违规过滤器\":38,\"02-规则执行-insurance-1-1-3\":10,\"02-规则执行-insurance-1-3-1\":12,\"01-数据处理\":2,\"01-数据处理-04费用冲正\":6,\"02-规则执行-insurance-2-1-5\":17,\"02-规则执行-insurance-2-1-4\":16,\"03-规则结果适配-02-第三方过滤器\":39,\"02-规则执行-insurance-2-1-3\":15,\"02-规则执行-insurance-2-1-2\":14,\"02-规则执行\":7,\"02-规则执行-insurance-2-1-7\":18,\"02-规则执行-insurance-3-1-9\":28,\"02-规则执行-insurance-x-1\":36,\"03-规则结果适配\":37,\"02-规则执行-insurance-2-1-1\":13,\"02-规则执行-insurance-3-1-2\":22,\"02-规则执行-insurance-3-2-1\":29,\"02-规则执行-insurance-3-1-1\":19,\"02-规则执行-insurance-3-1-4\":24,\"02-规则执行-insurance-3-2-3\":31,\"02-规则执行-insurance-3-3-2\":34,\"02-规则执行-insurance-3-1-3\":23,\"02-规则执行-insurance-3-2-2\":30,\"02-规则执行-insurance-3-3-1\":33,\"02-规则执行-insurance-3-1-6\":26,\"02-规则执行-insurance-3-1-5\":25,\"02-规则执行-insurance-3-2-4\":32,\"02-规则执行-insurance-3-3-3\":35,\"02-规则执行-insurance-3-1-8\":27,\"03-规则结果适配-03-数据落库\":40,\"02-规则执行-insurance-1-1-2\":9,\"02-规则执行-insurance-1-2-1\":11,\"02-规则执行-insurance-1-1-1\":8,\"01-数据处理-01取数\":3,\"02-规则执行-insurance-3-1-11\":21,\"02-规则执行-insurance-3-1-10\":20,\"00-总执行时长\":1}";
        String headMapStr = "{\"01-数据处理-02合并入参\":6,\"01-数据处理-03数据转换\":7,\"pid\":0,\"03-规则结果适配-01-违规过滤器\":38,\"02-规则执行-insurance-1-1-3\":11,\"02-规则执行-insurance-1-3-1\":13,\"01-数据处理\":2,\"01-数据处理-04费用冲正\":8,\"02-规则执行-insurance-2-1-5\":18,\"02-规则执行-insurance-2-1-4\":17,\"03-规则结果适配-02-第三方过滤器\":39,\"02-规则执行-insurance-2-1-3\":16,\"02-规则执行-insurance-2-1-2\":15,\"02-规则执行\":3,\"02-规则执行-insurance-2-1-7\":19,\"02-规则执行-insurance-3-1-9\":29,\"03-规则结果适配\":4,\"02-规则执行-insurance-x-1\":37,\"02-规则执行-insurance-2-1-1\":14,\"02-规则执行-insurance-3-1-2\":23,\"02-规则执行-insurance-3-2-1\":30,\"02-规则执行-insurance-3-1-1\":20,\"02-规则执行-insurance-3-1-4\":25,\"02-规则执行-insurance-3-2-3\":32,\"02-规则执行-insurance-3-3-2\":35,\"02-规则执行-insurance-3-1-3\":24,\"02-规则执行-insurance-3-2-2\":31,\"02-规则执行-insurance-3-3-1\":34,\"02-规则执行-insurance-3-1-6\":27,\"02-规则执行-insurance-3-1-5\":26,\"02-规则执行-insurance-3-2-4\":33,\"02-规则执行-insurance-3-3-3\":36,\"02-规则执行-insurance-3-1-8\":28,\"03-规则结果适配-03-数据落库\":40,\"02-规则执行-insurance-1-1-2\":10,\"02-规则执行-insurance-1-2-1\":12,\"02-规则执行-insurance-1-1-1\":9,\"01-数据处理-01取数\":5,\"02-规则执行-insurance-3-1-11\":22,\"02-规则执行-insurance-3-1-10\":21,\"00-总执行时长\":1}";
        HashMap<String,Integer> headMap = JSONObject.parseObject(headMapStr,HashMap.class);
//        String head = "\"pid\",\"总时长\",\"01-数据处理\",\"01-数据处理-01取数\",\"01-数据处理-02合并入参\",\"01-数据处理-03数据转换\",\"01-数据处理-04费用冲正\",\"02-规则执行\",\"02-规则执行-insurance-1-1-1\",\"02-规则执行-insurance-1-1-2\",\"02-规则执行-insurance-1-1-3\",\"02-规则执行-insurance-1-2-1\",\"02-规则执行-insurance-1-3-1\",\"02-规则执行-insurance-2-1-1\",\"02-规则执行-insurance-2-1-2\",\"02-规则执行-insurance-2-1-4\",\"02-规则执行-insurance-2-1-5\",\"02-规则执行-insurance-2-1-7\",\"02-规则执行-insurance-3-1-10\",\"02-规则执行-insurance-3-1-11\",\"02-规则执行-insurance-3-1-2\",\"02-规则执行-insurance-3-1-4\",\"02-规则执行-insurance-3-1-6\",\"02-规则执行-insurance-3-1-8\",\"02-规则执行-insurance-3-1-9\",\"02-规则执行-insurance-3-2-3\",\"02-规则执行-insurance-3-2-4\",\"02-规则执行-insurance-3-3-2\",\"02-规则执行-insurance-3-3-3\",\"02-规则执行-insurance-x-1\",\"03-规则结果适配\",\"03-规则结果适配-01-违规过滤器\",\"03-规则结果适配-02-第三方过滤器\",\"03-规则结果适配-03-数据落库\"";
        FileWriter fileWriter = new FileWriter(csvFile);
        fileWriter.write(head + "\n");
        InputStreamReader isr = null;
        try {
            isr = new InputStreamReader(new FileInputStream(logFile), "UTF8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        try (BufferedReader br = new BufferedReader(isr)) {
            String line;
            while ((line = br.readLine()) != null) {
                try {
                    String timeData = readTimeLine(line,headMap);
                    fileWriter.write(timeData + "\n");
                } catch (Exception e) {
                    System.out.println(line);
                    e.printStackTrace();
                }
            }
        }
        isr.close();
        fileWriter.close();
    }

    private String readTimeLine(String line,HashMap<String,Integer> headMap) {
        String[] dataStr = new String[headMap.size()];
        String[] temp1 = line.split("性能执行日志！pid:");
        String[] data = temp1[1].split(",执行时间:");
        String pid = data[0];
        String json = data[1].split("]")[0];
//        StringBuffer sb1 = new StringBuffer(pid + ",");
//        StringBuffer sb2 = new StringBuffer();
//        Long time = 0L;
        dataStr[headMap.get("pid")] = pid;
        JSONArray jsonArray = JSONArray.parseArray(json + "]");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String key = jsonObject.getString("key");
            Long execTime = jsonObject.getLong("execTime");
            dataStr[headMap.get(key)] = execTime.toString();
//            if(key.equals("01-数据处理")||key.equals("02-规则执行")||key.equals("03-规则结果适配")){
//                time+=execTime;
//            }
//            sb2.append("\"" + execTime + "\",");
        }
//        sb2.deleteCharAt(sb2.length() - 1);
        return Arrays.stream(dataStr).map(i->i==null?"0":i).collect(Collectors.joining(","));
    }

    public void getHead() throws IOException {
        String head = "[{\"endTime\":1689044581006,\"execTime\":20739,\"execTimeStr\":\"20S\",\"key\":\"00-总执行时长\",\"startTime\":1689044560267},{\"endTime\":1689044561826,\"execTime\":1559,\"execTimeStr\":\"1S\",\"key\":\"01-数据处理\",\"startTime\":1689044560267},{\"endTime\":1689044580390,\"execTime\":18564,\"execTimeStr\":\"18S\",\"key\":\"02-规则执行\",\"startTime\":1689044561826},{\"endTime\":1689044581006,\"execTime\":616,\"execTimeStr\":\"0S\",\"key\":\"03-规则结果适配\",\"startTime\":1689044580390},{\"endTime\":1689044561120,\"execTime\":850,\"execTimeStr\":\"0S\",\"key\":\"01-数据处理-01取数\",\"startTime\":1689044560270},{\"endTime\":1689044561120,\"execTime\":0,\"execTimeStr\":\"0S\",\"key\":\"01-数据处理-02合并入参\",\"startTime\":1689044561120},{\"endTime\":1689044561524,\"execTime\":404,\"execTimeStr\":\"0S\",\"key\":\"01-数据处理-03数据转换\",\"startTime\":1689044561120},{\"endTime\":1689044561826,\"execTime\":302,\"execTimeStr\":\"0S\",\"key\":\"01-数据处理-04费用冲正\",\"startTime\":1689044561524},{\"endTime\":1689044579605,\"execTime\":6,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-1-1-1\",\"startTime\":1689044579599},{\"endTime\":1689044579624,\"execTime\":6,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-1-1-2\",\"startTime\":1689044579618},{\"endTime\":1689044579965,\"execTime\":6,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-1-1-3\",\"startTime\":1689044579959},{\"endTime\":1689044580120,\"execTime\":6,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-1-2-1\",\"startTime\":1689044580114},{\"endTime\":1689044579657,\"execTime\":6,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-1-3-1\",\"startTime\":1689044579651},{\"endTime\":1689044579651,\"execTime\":7,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-2-1-1\",\"startTime\":1689044579644},{\"endTime\":1689044579936,\"execTime\":7,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-2-1-2\",\"startTime\":1689044579929},{\"endTime\":1689040300637,\"execTime\":1,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-2-1-3\",\"startTime\":1689040300636},{\"endTime\":1689044579928,\"execTime\":265,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-2-1-4\",\"startTime\":1689044579663},{\"endTime\":1689044579958,\"execTime\":22,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-2-1-5\",\"startTime\":1689044579936},{\"endTime\":1689044579663,\"execTime\":6,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-2-1-7\",\"startTime\":1689044579657},{\"endTime\":1689040300595,\"execTime\":0,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-1\",\"startTime\":1689040300595},{\"endTime\":1689044579611,\"execTime\":6,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-10\",\"startTime\":1689044579605},{\"endTime\":1689044579644,\"execTime\":20,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-11\",\"startTime\":1689044579624},{\"endTime\":1689044579389,\"execTime\":16388,\"execTimeStr\":\"16S\",\"key\":\"02-规则执行-insurance-3-1-2\",\"startTime\":1689044563001},{\"endTime\":1689040300587,\"execTime\":0,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-3\",\"startTime\":1689040300587},{\"endTime\":1689044562982,\"execTime\":66,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-4\",\"startTime\":1689044562916},{\"endTime\":1689040300606,\"execTime\":5,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-5\",\"startTime\":1689040300601},{\"endTime\":1689044579530,\"execTime\":67,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-6\",\"startTime\":1689044579463},{\"endTime\":1689044563000,\"execTime\":18,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-8\",\"startTime\":1689044562982},{\"endTime\":1689044579617,\"execTime\":5,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-1-9\",\"startTime\":1689044579612},{\"endTime\":1689044580357,\"execTime\":138,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-2-1\",\"startTime\":1689044580219},{\"endTime\":1689044580218,\"execTime\":97,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-2-2\",\"startTime\":1689044580121},{\"endTime\":1689044562419,\"execTime\":113,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-2-3\",\"startTime\":1689044562306},{\"endTime\":1689044579461,\"execTime\":72,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-2-4\",\"startTime\":1689044579389},{\"endTime\":1689044580112,\"execTime\":147,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-3-1\",\"startTime\":1689044579965},{\"endTime\":1689044562305,\"execTime\":104,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-3-2\",\"startTime\":1689044562201},{\"endTime\":1689044579598,\"execTime\":67,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-3-3-3\",\"startTime\":1689044579531},{\"endTime\":1689044562915,\"execTime\":495,\"execTimeStr\":\"0S\",\"key\":\"02-规则执行-insurance-x-1\",\"startTime\":1689044562420},{\"endTime\":1689044580990,\"execTime\":175,\"execTimeStr\":\"0S\",\"key\":\"03-规则结果适配-01-违规过滤器\",\"startTime\":1689044580815},{\"endTime\":1689044580990,\"execTime\":0,\"execTimeStr\":\"0S\",\"key\":\"03-规则结果适配-02-第三方过滤器\",\"startTime\":1689044580990},{\"endTime\":1689044581005,\"execTime\":15,\"execTimeStr\":\"0S\",\"key\":\"03-规则结果适配-03-数据落库\",\"startTime\":1689044580990}]";
        StringBuffer headStr = new StringBuffer();
        JSONArray jsonArray = JSONArray.parseArray(head);
        HashMap<String,Integer> headMap = new HashMap<>();
        headMap.put("pid",0);
        headStr.append("\"pid\",");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            headMap.put(jsonObject.getString("key"),i+1);
            headStr.append("\"").append(jsonObject.getString("key")).append("\",");
        }
        System.out.println(headStr.toString());
        System.out.println(JSONObject.toJSONString(headMap));


    }
}
